package com.apps.gomdiem.adapter;



import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.gomdiem.R;
import com.apps.gomdiem.model.Store;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;


public class GridAdapter extends BaseAdapter {
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	ArrayList<Store> arrayStore;
	Context context;
	
	public  GridAdapter(ArrayList<Store> textArr,Context contxt){
		arrayStore=textArr;
		context=contxt;
		options = new DisplayImageOptions.Builder()
		  .cacheInMemory(true)
		  .cacheOnDisc(true)
		  .displayer(new SimpleBitmapDisplayer())
		  .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
		  .showStubImage(R.drawable.ic_launcher)
		  .showImageOnFail(R.drawable.ic_launcher)
		  .showImageForEmptyUri(R.drawable.ic_launcher)
		  .bitmapConfig(Bitmap.Config.RGB_565)
		  .build();
		
	}
	
	public void update(ArrayList<Store> textArr) {
		this.arrayStore = textArr;
		this.notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrayStore.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return arrayStore.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	 public View getView(int position, View convertView, ViewGroup parent) {

	  ViewHolder viewHolder;
	  if (convertView == null) {
	   LayoutInflater ltInflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	   viewHolder = new ViewHolder();
	   convertView = ltInflate.inflate(R.layout.custom_gridview_item, null);
	   viewHolder.img = (ImageView) convertView.findViewById(R.id.grid_img);
	   viewHolder.txtText= (TextView) convertView.findViewById(R.id.textViewNameStore);
	   convertView.setTag(viewHolder);

	  } else {
	   viewHolder = (ViewHolder) convertView.getTag();
	  }
	  viewHolder.txtText.setText(arrayStore.get(position).nameStore);
	  imageLoader.displayImage(arrayStore.get(position).linkImg, viewHolder.img, options);
	  String pIdStore=arrayStore.get(position).idStore;
	  viewHolder.idStore=pIdStore;
	  return convertView;
	 }
	 
	 public class ViewHolder{
	  ImageView img;
	  TextView txtText;
	  String idStore;
	   public String getIdStore() {
	       return idStore;
	   }
	 }

}

